package com.inei.cenacom.model.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;

import com.inei.cenacom.controller.Cap100Controller;
import com.inei.cenacom.controller.Cap200Controller;
import com.inei.cenacom.controller.Cap300Controller;
import com.inei.cenacom.controller.Cap400Controller;
import com.inei.cenacom.model.entity.Cap100;
import com.inei.cenacom.model.entity.Cap200;
import com.inei.cenacom.model.entity.Cap300;
import com.inei.cenacom.model.entity.Cap400;

public class Cap200Dao extends ACRUD<Cap200> {
	public Context context;

	public Cap200Dao(Context context) {
		super(context);
		this.context = context;
	}

	@Override
	public List<Cap200> leerTodo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Cap200> leer(String id, int anio) {

		List<Cap200> lista = null;
		db = util.getReadableDatabase();
		c = db.rawQuery("select * from CAP200 where ID_N=? and ANIO=?",
				new String[] { id, anio + "" });
		if (c.moveToFirst()) {
			lista = new ArrayList<Cap200>();
			do {
				Cap200 o = new Cap200();
				o.setID_N(id);
				o.setANIO(anio);
				o.setNRO_MREG(c.getInt(c.getColumnIndex("NRO_MREG")));
				o.setIH201(c.getInt(c.getColumnIndex("IH201")));
				o.setIH201_O(c.getString(c.getColumnIndex("IH201_O")));
				o.setIH202(c.getInt(c.getColumnIndex("IH202")));
				o.setIH202_O(c.getString(c.getColumnIndex("IH202_O")));
				o.setIH203(c.getInt(c.getColumnIndex("IH203")));
				o.setIH203_O(c.getString(c.getColumnIndex("IH203_O")));
				o.setIH_NRO_DOC(c.getString(c.getColumnIndex("IH_NRO_DOC")));
				o.setIH204(c.getInt(c.getColumnIndex("IH204")));
				o.setIH204_O(c.getString(c.getColumnIndex("IH204_O")));
				o.setIH205_DIA(c.getString(c.getColumnIndex("IH205_DIA")));
				o.setIH205_MES(c.getString(c.getColumnIndex("IH205_MES")));
				o.setIH206_HOR(c.getString(c.getColumnIndex("IH206_HOR")));
				o.setIH206_MIN(c.getString(c.getColumnIndex("IH206_MIN")));
				o.setIH207(c.getInt(c.getColumnIndex("IH207")));
				o.setIH207_O(c.getString(c.getColumnIndex("IH207_O")));
				o.setIH208(c.getInt(c.getColumnIndex("IH208")));
				o.setIH208_O(c.getString(c.getColumnIndex("IH208_O")));
				o.setIH209_1(c.getInt(c.getColumnIndex("IH209_1")));
				o.setIH209_2(c.getInt(c.getColumnIndex("IH209_2")));
				o.setIH209_3(c.getInt(c.getColumnIndex("IH209_3")));
				o.setIH209_4(c.getInt(c.getColumnIndex("IH209_4")));
				o.setIH209_5(c.getInt(c.getColumnIndex("IH209_5")));
				o.setIH209_6(c.getInt(c.getColumnIndex("IH209_6")));
				o.setIH209_7(c.getInt(c.getColumnIndex("IH209_7")));
				o.setIH209_8(c.getInt(c.getColumnIndex("IH209_8")));
				o.setIH209_9(c.getInt(c.getColumnIndex("IH209_9")));
				o.setIH209_10(c.getInt(c.getColumnIndex("IH209_10")));
				o.setIH209_11(c.getInt(c.getColumnIndex("IH209_11")));
				o.setIH209_12(c.getInt(c.getColumnIndex("IH209_12")));
				o.setIH209_13(c.getInt(c.getColumnIndex("IH209_13")));
				o.setIH209_14(c.getInt(c.getColumnIndex("IH209_14")));
				o.setIH209_15(c.getInt(c.getColumnIndex("IH209_15")));
				o.setIH209_16(c.getInt(c.getColumnIndex("IH209_16")));
				o.setIH209_16_O(c.getString(c.getColumnIndex("IH209_16_O")));
				o.setIH210(c.getInt(c.getColumnIndex("IH210")));
				o.setIH211(c.getInt(c.getColumnIndex("IH211")));
				o.setIH211_O(c.getString(c.getColumnIndex("IH211_O")));
				o.setIH212(c.getInt(c.getColumnIndex("IH212")));
				o.setIH213(c.getInt(c.getColumnIndex("IH213")));
				o.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
				o.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
				o.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
				o.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
				o.setOBS_2(c.getString(c.getColumnIndex("OBS_2")));
				lista.add(o);
			} while (c.moveToNext());
		} else {
			lista = null;
		}
		cerrar();
		return lista;
	}

	@Override
	public Cap200 buscar(Cap200 o) {
		db = util.getReadableDatabase();
		c = db.rawQuery(
				"select * from CAP200 where ID_N=? and ANIO=? and NRO_MREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "" });
		if (c.moveToFirst()) {
			o.setIH201(c.getInt(c.getColumnIndex("IH201")));
			o.setIH201_O(c.getString(c.getColumnIndex("IH201_O")));
			o.setIH202(c.getInt(c.getColumnIndex("IH202")));
			o.setIH202_O(c.getString(c.getColumnIndex("IH202_O")));
			o.setIH203(c.getInt(c.getColumnIndex("IH203")));
			o.setIH203_O(c.getString(c.getColumnIndex("IH203_O")));
			o.setIH204(c.getInt(c.getColumnIndex("IH204")));
			o.setIH204_O(c.getString(c.getColumnIndex("IH204_O")));
			o.setIH_NRO_DOC(c.getString(c.getColumnIndex("IH_NRO_DOC")));
			o.setIH205_DIA(c.getString(c.getColumnIndex("IH205_DIA")));
			o.setIH205_MES(c.getString(c.getColumnIndex("IH205_MES")));
			o.setIH206_HOR(c.getString(c.getColumnIndex("IH206_HOR")));
			o.setIH206_MIN(c.getString(c.getColumnIndex("IH206_MIN")));
			o.setIH207(c.getInt(c.getColumnIndex("IH207")));
			o.setIH207_O(c.getString(c.getColumnIndex("IH207_O")));
			o.setIH208(c.getInt(c.getColumnIndex("IH208")));
			o.setIH208_O(c.getString(c.getColumnIndex("IH208_O")));
			o.setIH209_1(c.getInt(c.getColumnIndex("IH209_1")));
			o.setIH209_2(c.getInt(c.getColumnIndex("IH209_2")));
			o.setIH209_3(c.getInt(c.getColumnIndex("IH209_3")));
			o.setIH209_4(c.getInt(c.getColumnIndex("IH209_4")));
			o.setIH209_5(c.getInt(c.getColumnIndex("IH209_5")));
			o.setIH209_6(c.getInt(c.getColumnIndex("IH209_6")));
			o.setIH209_7(c.getInt(c.getColumnIndex("IH209_7")));
			o.setIH209_8(c.getInt(c.getColumnIndex("IH209_8")));
			o.setIH209_9(c.getInt(c.getColumnIndex("IH209_9")));
			o.setIH209_10(c.getInt(c.getColumnIndex("IH209_10")));
			o.setIH209_11(c.getInt(c.getColumnIndex("IH209_11")));
			o.setIH209_12(c.getInt(c.getColumnIndex("IH209_12")));
			o.setIH209_13(c.getInt(c.getColumnIndex("IH209_13")));
			o.setIH209_14(c.getInt(c.getColumnIndex("IH209_14")));
			o.setIH209_15(c.getInt(c.getColumnIndex("IH209_15")));
			o.setIH209_16(c.getInt(c.getColumnIndex("IH209_16")));
			o.setIH209_16_O(c.getString(c.getColumnIndex("IH209_16_O")));
			o.setIH210(c.getInt(c.getColumnIndex("IH210")));
			o.setIH211(c.getInt(c.getColumnIndex("IH211")));
			o.setIH211_O(c.getString(c.getColumnIndex("IH211_O")));
			o.setIH212(c.getInt(c.getColumnIndex("IH212")));
			o.setIH213(c.getInt(c.getColumnIndex("IH213")));
			o.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
			o.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
			o.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
			o.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
			o.setOBS_2(c.getString(c.getColumnIndex("OBS_2")));
		} else {
			o = null;
		}
		cerrar();
		return o;
	}

	@Override
	public int crear(Cap200 o) {
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv.put("ID_N", o.getID_N());
		cv.put("ANIO", o.getANIO());
		cv.put("NRO_MREG", o.getNRO_MREG());
		cv.put("IH201", o.getIH201());
		cv.put("IH201_O", o.getIH201_O());
		cv.put("IH202", o.getIH202());
		cv.put("IH202_O", o.getIH202_O());
		cv.put("IH203", o.getIH203());
		cv.put("IH203_O", o.getIH203_O());
		cv.put("IH204", o.getIH204());
		cv.put("IH204_O", o.getIH204_O());
		cv.put("IH_NRO_DOC", o.getIH_NRO_DOC());
		cv.put("IH205_DIA", o.getIH205_DIA());
		cv.put("IH205_MES", o.getIH205_MES());
		cv.put("IH206_HOR", o.getIH206_HOR());
		cv.put("IH206_MIN", o.getIH206_MIN());
		cv.put("IH207", o.getIH207());
		cv.put("IH207_O", o.getIH207_O());
		cv.put("IH208", o.getIH208());
		cv.put("IH208_O", o.getIH208_O());
		cv.put("IH209_1", o.getIH209_1());
		cv.put("IH209_2", o.getIH209_2());
		cv.put("IH209_3", o.getIH209_3());
		cv.put("IH209_4", o.getIH209_4());
		cv.put("IH209_5", o.getIH209_5());
		cv.put("IH209_6", o.getIH209_6());
		cv.put("IH209_7", o.getIH209_7());
		cv.put("IH209_8", o.getIH209_8());
		cv.put("IH209_9", o.getIH209_9());
		cv.put("IH209_10", o.getIH209_10());
		cv.put("IH209_11", o.getIH209_11());
		cv.put("IH209_12", o.getIH209_12());
		cv.put("IH209_13", o.getIH209_13());
		cv.put("IH209_14", o.getIH209_14());
		cv.put("IH209_15", o.getIH209_15());
		cv.put("IH209_16", o.getIH209_16());
		cv.put("IH209_16_O", o.getIH209_16_O());
		cv.put("IH210", o.getIH210());
		cv.put("IH211", o.getIH211());
		cv.put("IH211_O", o.getIH211_O());
		cv.put("IH212", o.getIH212());
		cv.put("IH213", o.getIH213());
		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		cv.put("USR_CREACION", o.getUSR_CREACION());
		cv.put("FEC_CREACION", date);
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);
		cv.put("OBS_2", o.getOBS_2());

		int res = (int) db.insert("CAP200", null, cv);
		if (res != -1) {
			db.setTransactionSuccessful();
		}

		cerrar();

		return res;
	}

	@Override
	public int actualizar(Cap200 o) {
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv.put("IH201", o.getIH201());
		cv.put("IH201_O", o.getIH201_O());
		cv.put("IH202", o.getIH202());
		cv.put("IH202_O", o.getIH202_O());
		cv.put("IH203", o.getIH203());
		cv.put("IH203_O", o.getIH203_O());
		cv.put("IH204", o.getIH204());
		cv.put("IH204_O", o.getIH204_O());
		cv.put("IH_NRO_DOC", o.getIH_NRO_DOC());
		cv.put("IH205_DIA", o.getIH205_DIA());
		cv.put("IH205_MES", o.getIH205_MES());
		cv.put("IH206_HOR", o.getIH206_HOR());
		cv.put("IH206_MIN", o.getIH206_MIN());
		cv.put("IH207", o.getIH207());
		cv.put("IH207_O", o.getIH207_O());
		cv.put("IH208", o.getIH208());
		cv.put("IH208_O", o.getIH208_O());
		cv.put("IH209_1", o.getIH209_1());
		cv.put("IH209_2", o.getIH209_2());
		cv.put("IH209_3", o.getIH209_3());
		cv.put("IH209_4", o.getIH209_4());
		cv.put("IH209_5", o.getIH209_5());
		cv.put("IH209_6", o.getIH209_6());
		cv.put("IH209_7", o.getIH209_7());
		cv.put("IH209_8", o.getIH209_8());
		cv.put("IH209_9", o.getIH209_9());
		cv.put("IH209_10", o.getIH209_10());
		cv.put("IH209_11", o.getIH209_11());
		cv.put("IH209_12", o.getIH209_12());
		cv.put("IH209_13", o.getIH209_13());
		cv.put("IH209_14", o.getIH209_14());
		cv.put("IH209_15", o.getIH209_15());
		cv.put("IH209_16", o.getIH209_16());
		cv.put("IH209_16_O", o.getIH209_16_O());
		cv.put("IH210", o.getIH210());
		cv.put("IH211", o.getIH211());
		cv.put("IH211_O", o.getIH211_O());
		cv.put("IH212", o.getIH212());
		cv.put("IH213", o.getIH213());
		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		if (o.getFEC_CREACION() == null) {
			cv.put("FEC_CREACION", date);
		} else if (o.getFEC_CREACION().equals("")) {
			cv.put("FEC_CREACION", date);
		}
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);
		cv.put("OBS_2", o.getOBS_2());
		int res = (int) db.update("CAP200", cv,
				"ID_N=? and ANIO=? and NRO_MREG=?", new String[] { o.getID_N(),
						o.getANIO() + "", o.getNRO_MREG() + "" });
		if (res > 0) {
			db.setTransactionSuccessful();
		}

		cerrar();

		return res;
	}

	@Override
	public int eliminar(Cap200 o) {
		int res = 0;
		Cap100Controller con100 = new Cap100Controller(context);
		Cap100 c100 = new Cap100();
		c100.setID_N(o.getID_N());
		c100.setANIO(o.getANIO());
		c100 = con100.buscar(c100);
		if (c100 != null) {
			int v204 = o.getIH204();

			switch (v204) {
			case 1:
				c100.setDN101_1_A(c100.getDN101_1_A() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 2:
				c100.setDN101_1_B(c100.getDN101_1_B() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 3:
				c100.setDN101_1_C(c100.getDN101_1_C() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 4:
				c100.setDN101_1_D(c100.getDN101_1_D() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 5:
				c100.setDN101_1_E(c100.getDN101_1_E() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 6:
				c100.setDN101_1_F(c100.getDN101_1_F() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 7:
				c100.setDN101_1_G(c100.getDN101_1_G() - 1);
				c100.setDN101_1(c100.getDN101_1() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 8:
			case 9:
				c100.setDN101_2(c100.getDN101_2() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 10:
				c100.setDN101_3(c100.getDN101_3() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 11:
				c100.setDN101_4(c100.getDN101_4() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 12:
			case 13:
			case 14:
			case 15:
				c100.setDN101_5(c100.getDN101_5() - 1);
				c100.setDN101(c100.getDN101() - 1);
				c100.setDN120(c100.getDN120() - 1);
				break;
			case 16:
			case 17:
			case 18:
			case 19:
			case 20:
			case 21:
			case 22:
				c100.setDN101_6(c100.getDN101_6() - 1);
				c100.setDN121(c100.getDN121() - 1);
				break;
			default:
				break;
			}
			con100.actualizar(c100);

			db = util.getWritableDatabase();
			res = db.delete(
					"CAP200",
					"ID_N=? and ANIO=? and NRO_MREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "" });
			db.delete(
					"CAP300",
					"ID_N=? and ANIO=? and NRO_MREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "" });
			db.delete(
					"CAP400",
					"ID_N=? and ANIO=? and NRO_MREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "" });
			db.close();

			Cap200Controller con200 = new Cap200Controller(context);
			Cap300Controller con300 = new Cap300Controller(context);
			Cap400Controller con400 = new Cap400Controller(context);

			List<Cap200> l200 = con200.leer(o.getID_N(), o.getANIO());
			if (l200 != null) {

				db = util.getWritableDatabase();
				for (int i = 0; i < l200.size(); i++) {
					int pos = i + 1;
					Cap200 c200 = l200.get(i);

					cv = new ContentValues();
					cv.put("NRO_MREG", pos);
					db.update("CAP200", cv,"ID_N=? and ANIO=? and NRO_MREG=?",new String[] { c200.getID_N(), c200.getANIO() + "",
							c200.getNRO_MREG() + "" });
					

					db.update("CAP300", cv,"ID_N=? and ANIO=? and NRO_MREG=?",new String[] { c200.getID_N(), c200.getANIO() + "",
							c200.getNRO_MREG() + "" });

					db.update("CAP400", cv,"ID_N=? and ANIO=? and NRO_MREG=?",new String[] { c200.getID_N(), c200.getANIO() + "",
							c200.getNRO_MREG() + "" });
				}

				db.close();
			}

		}
		return res;
	}

	public List<Cap200> leer(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public String obtenerDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(Calendar.getInstance().getTime());
	}
}
